package com.hula.onion.book.server.spider.platforms.yonglibao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.spider.platforms.xiangshangjinfu.XiangShangJinFuSpiderHelper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/1/26.
 */
@Component
public class YongLiBaoSpiderHelper {

    private static final Logger logger = LoggerFactory.getLogger(YongLiBaoSpiderHelper.class);

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(5);
        map.put("username", form.getUserName());
        map.put("password", form.getPassword());
        map.put("captcha", "");
        map.put("captcha_ticket", "");
        map.put("device_type", "5");
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public CommonResponse parseAccountBalance(String json) {
        if (StringUtils.isEmpty(json)) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        JSONObject accountJson = JSON.parseObject(json);
        if ("1000".equals(accountJson.getString("code"))) {
            try {
                AccountInfo accountInfo = new AccountInfo();
                BigDecimal amount = accountJson.getJSONObject("data").getBigDecimal("assets");
                BigDecimal totalIntrest = accountJson.getJSONObject("data").getBigDecimal("total_profit");
                BigDecimal waitIntrest = accountJson.getJSONObject("data").getBigDecimal("frozen_earning");
                accountInfo.setAmount(amount);
                accountInfo.setTotalIntrest(totalIntrest);
                accountInfo.setWaitIntrest(waitIntrest);
                return new CommonResponse(accountInfo);
            } catch (Exception e) {
                logger.error("[XinYongBaoSpiderHelper] parseAccountBalance fail!", e);
            }
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
